#include "FlashPlayerBackend.h"
#include <QApplication>
#include <QFile>
namespace Douban
{
	FlashPlayerBackend::FlashPlayerBackend()
	{
		b_isFunctional = m_axobj.setControl("d27cdb6e-ae6d-11cf-96b8-444553540000");
		if(b_isFunctional)
		{
			QString file = qApp->applicationDirPath() + "/data/fmplayer.swf";
			QFile::remove(file);
			QFile::copy(":/Douban/player.swf",file);
			m_axobj.dynamicCall("LoadMovie(long,string)",0,file);
			connect(&m_axobj,SIGNAL(FlashCall(QString)),this,SLOT(FlashCall(QString)));
		}
	}

	FlashPlayerBackend::~FlashPlayerBackend(){}

	bool FlashPlayerBackend::isFunctional()
	{
		return b_isFunctional;
	}

	void FlashPlayerBackend::FlashCall(QString s)
	{
		if(s.contains("onPlayStart"))
		{
			emit playStart();
		}else if(s.contains("onPlayComplete"))
		{
			emit playComplete();
		}else if(s.contains("onPlayError"))
		{
			emit playError();
		}
	}

	void FlashPlayerBackend::play(const QString& url)
	{
		if(!b_isFunctional)
			return;
		QString invokeText = "<invoke name=\"playMusic\" returntype=\"xml\"><arguments><string>%1</string></arguments></invoke>";
		m_axobj.dynamicCall("CallFunction(string)",invokeText.arg(url));
	}

	void FlashPlayerBackend::stop()
	{
		if(!b_isFunctional)
			return;
		QString invokeText = "<invoke name=\"stopMusic\" returntype=\"xml\"><arguments></arguments></invoke>";
		m_axobj.dynamicCall("CallFunction(string)",invokeText);
	}

	void FlashPlayerBackend::setVolume(int vol)
	{
		if(!b_isFunctional)
			return;
		QString invokeText = "<invoke name=\"setVolume\" returntype=\"xml\"><arguments><number>%1</number></arguments></invoke>";
		m_axobj.dynamicCall("CallFunction(string)",invokeText.arg(vol));
	}

} // namespace Douban
